package Client;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;

public class ContactsManager {
	private Set<Contact> contactsList = new LinkedHashSet<Contact>();
	String filename;
	
	public ContactsManager() {
		this("contacts.db");
	}
	
	public ContactsManager(String filename) {
		File f = new File(filename);
		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				System.out.println("Couldn't create file.");
			}
		}
		load(filename);
	}
	
	public boolean add(Contact c) {
		boolean succeed;
		succeed = contactsList.add(c);
		if (!succeed) {
			return false;
		}
		try {
			FileWriter fw = new FileWriter(filename,true);
			PrintWriter pw = new PrintWriter(fw);
			pw.println(c.getNumber() + " " + c.getName());
			pw.flush();
			pw.close();
			return true;
		} catch (IOException e) {
			contactsList.remove(c);
			System.out.println("User add failed.");
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean remove(Contact c) {
		boolean succeed;
		succeed = contactsList.remove(c);
		if (succeed) {
			updateDbFile();
		}
		return succeed;
	}
	
	public void load(String filename) {
		if (filename.isEmpty()) {
			throw new IllegalArgumentException(
					"Filename can't be empty.");
		}
		File dbfile = new File(filename);
		if (!dbfile.exists()) {
			throw new IllegalArgumentException(
					"Wrong filename.");
		}
		this.filename = filename;
		contactsList.clear();
		try {
			Scanner s = new Scanner(dbfile);
			String line;
			String[] splitted;
			String name;
			String number;
			while (s.hasNextLine()) {
				line = s.nextLine();
				splitted = line.split(" ");
				number = splitted[0];
				name = splitted[1];
				contactsList.add(
						new Contact(Long.valueOf(number), name));
			}
			s.close();
		} catch (IOException e) {
			System.out.println("Couldn't create database file.");
			e.printStackTrace();
		}
	}
	
	public Set<Contact> getContactsList() {
		return contactsList;
	}
	
	public Contact getContact(long number) {
		for (Contact c : contactsList) {
			if (c.getNumber() == number) {
				return c;
			}
		}
		return null;
	}
	
	public Contact getContact(String name) {
		for (Contact c : contactsList) {
			if (c.getName() == name) {
				return c;
			}
		}
		return null;
	}
	
	public String getName(long number) {
		String result = Long.toString(number);
		for (Contact c : contactsList) {
			if (c.getNumber() == number) {
				result = c.getName();
			}
		}
		return result;
	}
	
	public long getNumber(String name) {
		long result = -1;
		for (Contact c : contactsList) {
			if (c.getName().equals(name)) {
				result = c.getNumber();
			}
		}
		return result;
	}
	
	private void updateDbFile() {
		File f = new File(filename);
		f.delete();
		f = new File(filename);
		try {
			PrintWriter fw = new PrintWriter(f);
			for (Contact c : contactsList) {
				fw.println(c.getNumber() + " " + c.getName());
			}
			fw.flush();
			fw.close();
		} catch (IOException e) {
			System.out.println("Contacts update failed.");
			e.printStackTrace();
		}
	}
}
